package com.wnxy.pawnbackgroundsystem.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.pawnbackgroundsystem.dto.PropertyPageDto;
import com.wnxy.pawnbackgroundsystem.entity.Property;
import com.wnxy.pawnbackgroundsystem.entity.Sort;
import com.wnxy.pawnbackgroundsystem.param.AddPropertyParam;
import com.wnxy.pawnbackgroundsystem.param.SelectPropertyParam;
import com.wnxy.pawnbackgroundsystem.param.UpdatePropertyParam;
import com.wnxy.pawnbackgroundsystem.response.Result;
import com.wnxy.pawnbackgroundsystem.service.IPropertyService;
import com.wnxy.pawnbackgroundsystem.service.ISortService;
import com.wnxy.pawnbackgroundsystem.service.impl.SortServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品属性表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@RestController
@RequestMapping("/pawnbackgroundsystem/property")
@Api(tags = "商品属性管理接口")
@CrossOrigin("*")
public class PropertyController {
    @Resource
    private IPropertyService propertyService;
    @Resource
    private ISortService sortService;

    @ApiOperation("分页模糊查询商品属性")
    @PostMapping("/selectProperty")
    public Result selectProperty(@RequestBody SelectPropertyParam spp){
//        PropertyPageDto pd=propertyService.selectProperty(spp);
        List<Property> select = propertyService.select(spp);
        IPage<Property> objectPage = new Page<>();
        int count = propertyService.count();

        objectPage.setSize(select.size());
        objectPage.setTotal(count);
        objectPage.setRecords(select);
        return new Result(true,"2002","查询成功",objectPage);
    }

    @ApiOperation("商品属性修改")
    @PostMapping("/updateProperty")
    public Result updateProperty(@RequestBody UpdatePropertyParam upp){
        propertyService.updateProperty(upp);

        return new Result(true,"2002","修改成功",null);
    }


    @ApiOperation("商品属性删除")
    @GetMapping("/deleteProperty")
    public Result deleteProperty(@RequestParam("propertyId")  Integer propertyId){
        propertyService.deleteProperty(propertyId);
        return new Result(true,"2002","删除成功",null);
    }

    @ApiOperation("商品属性新增")
    @PostMapping("/add")
    public Result insertProperty(@RequestBody AddPropertyParam addPropertyParam) throws ParseException {
        System.out.println(addPropertyParam);
        propertyService.addProperty(addPropertyParam);
        return new Result(true,"2002","新增成功",null);
    }

    @ApiOperation("查询商品大类")
    @GetMapping("/getSort")
    public Result getSort(){
        List<Sort> sort=sortService.list(new QueryWrapper<Sort>().eq("sort_grade","一级"));
//        List<String> name=new ArrayList<>();
//        for (Sort sort1 : sort) {
//            name.add(sort1.getSortName());
//        }
        return new Result(true,"2002","查询成功",sort);
    }


    @ApiOperation("查询属性名称")
    @GetMapping("/getPropertyName")
    public Result getPropertyName(){
       List<Property> name= propertyService.getPropertyName();
        return new Result(true,"2002","查询成功",name);
    }

    //根据商品大类id删除属性
    @DeleteMapping("/delManyProperty")
    public Result delManyProperty(@RequestParam(value = "intList") List<Integer> intList){
        propertyService.delManyProperty(intList);
        return new Result(true,"2002","删除成功",null);
    }


}

